
var interval_id = null;
var sum_id = null;
var begintime = null;
var total_time = null;
const db = wx.cloud.database();
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text_color: "#ffffff",
    background_color: "#f2c512",
    navtitle: "考研上岸室",
    
    hour: "00",
    minute: "00",
    second: "00",

    start_time: "",
    end_time: "",
    status: false,
    totalList:[],
    Sum : 0

    

  },

/**
   * 把上一界面的参数颜色传过来
   */
  onLoad: function (options) {
    this.setData({
      //text_color: options.text_color,
      background_color: options.background_color,
      navtitle: options.navtitle
    });

    var navigation_text_color = options.background_color == "#ffffff" ? "#000000": "#ffffff";

    wx.setNavigationBarColor({
      frontColor: navigation_text_color,
      backgroundColor: this.data.background_color,
    });

    wx.setNavigationBarTitle({
      title: this.data.navtitle,
    });
    /*保持常亮*/ 
    wx.setKeepScreenOn({
      keepScreenOn: true,
    });


    this.updateTime();

    var that = this;
    interval_id = setInterval(function(){that.updateTime()}, 1000);

    var that = this;
    wx.getStorage({
      key: 'totalList',
      success :function (res) {
        for (let i in res.data) {
          that.data.totalList.push(res.data[i])
        };
      }
    })

    this.showDialog()

    this._onRecordstarttime()
  },


  /*弹窗--开始*/

  showDialog: function () {
    this.setData({
    showModal: true
    })
    },
    /** 
    * 弹出框蒙层截断touchmove事件
    */ 
  preventTouchMove: function () {
    },
     
    /**
    * 隐藏模态对话框
    */
     
  hideModal: function () { 
    this.setData({
    showModal: false 
    });
     
    },
     
    /**
     
    * 对话框确认按钮点击事件
     
    */
     
  onConfirm: function () {    
    wx.showToast({   
    title: '开始学习',    
    icon: 'success', 
    duration: 1000  
    })    
    this.hideModal(); 
    let that = this;
    sum_id = setInterval(function(){that.onSearch()}, 2000);

    },


    /*弹窗--结束*/

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //this.onUpdate()
    this.onSearch()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onUpdate()
  },
  onUpdate(){
    db.collection('sum').doc('1229caae5ef096e90033392b255ad213').update({
      data: {
        sum: _.inc(1)
      },
      success: function(res) {
        //console.log(res)
      }
    })
  },
  onSearch(){
    let that = this;
    
    db.collection('sum').doc('1229caae5ef096e90033392b255ad213').get({
      success: function(res) {
        // res.data 包含该记录的数据
        //console.log(res.data.sum);
        var sum = 0;
        sum = res.data.sum
        that.setData({
          Sum: sum
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.onDelete()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(interval_id);
    clearInterval(sum_id);
    wx.setKeepScreenOn({
      keepScreenOn: false,
    });

    this.onDelete()

    this._onRecordtotaltime()
  },
  onDelete(){
    
    db.collection('sum').doc('1229caae5ef096e90033392b255ad213').update({
      data: {
        sum: _.inc(-1)
      },
      success: function(res) {
        //console.log(res)
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '高颜值自习神器',
      imageUrl: 'cloud://bluewave-7co1p.626c-bluewave-7co1p-1301720800/image/share.png'
    }

  },

  _onRecordstarttime: function () {
    //记录当前学习时间
    var start_time = new Date();
    var h = start_time.getHours();
    var m = start_time.getMinutes();
    //console.log(h);
    //console.log(m);
    begintime = h + m/60;
    //console.log(begintime)
  },

  _onRecordtotaltime(){
    //记录结束时间
    var end_time = new Date();
    var x = end_time.getHours();
    var y = end_time.getMinutes();
    //console.log(x);
    //console.log(y);
    //console.log(end_time)
    var total_time = x + y/60 - (begintime);
    total_time = total_time.toFixed(2);
    if (total_time < 0)
    {
      total_time = 24 - begintime + x + y/60
    }
    
    this.data.totalList.push(total_time);
    //wx.setStorageSync("total_time", total_time);
    wx.setStorageSync('totalList', this.data.totalList)
    wx.getStorage({
      key: 'totalList',
      success :function (res) {
        //console.log(res)
      }
    })
  
  },



  updateTime: function() {
    var current_time = new Date();

    this.setData({
      hour: ('0' + current_time.getHours()).slice(-2),
      minute: ('0' + current_time.getMinutes()).slice(-2),
      second: ('0' + current_time.getSeconds()).slice(-2)
    });

  }
})